/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.Media;
import org.crystalrange.enterprise.model.Pricelist;
import org.crystalrange.enterprise.service.MediaService;
import org.crystalrange.enterprise.service.PricelistService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Iframe;
import org.zkoss.zul.Messagebox;

public class PricelistPrintoutController extends BaseController {

    protected MediaService mediaService;
    protected PricelistService pricelistService;
    protected Pricelist pricelist;
    protected Iframe iframePricelist;
    protected AMedia amedia;

    public MediaService getMediaService() {
        return this.mediaService;
    }

    public void setMediaService(MediaService mediaService) {
        this.mediaService = mediaService;
    }

    public PricelistService getPricelistService() {
        return this.pricelistService;
    }

    public void setPricelistService(PricelistService pricelistService) {
        this.pricelistService = pricelistService;
    }

    public Pricelist getPricelist() {
        return this.pricelist;
    }

    public void setPricelist(Pricelist pricelist) {
        this.pricelist = pricelist;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.pricelist = this.pricelistService.find(new Long(this.execution.getParameter("Id")));
        this.labelTitle.setValue("Printout Pricelist: " + this.pricelist.getName());

        File reportFile = new File(application.getRealPath("reports/pricelist.jasper"));

        Map parameters = new HashMap();
        parameters.put("PRICELIST", "zażółć gęślą jaźń:");

        ArrayList<Pricelist> pricelists = new ArrayList<Pricelist>();
        pricelists.add(pricelist);

        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(pricelists);
        
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getAbsolutePath(), parameters, dataSource);
        
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        
        JRExporter exporter = new JRPdfExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, arrayOutputStream);
        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

        exporter.exportReport();
        
        arrayOutputStream.close();

        this.amedia = new AMedia("pricelist.pdf", "pdf", "application/pdf", arrayOutputStream.toByteArray());

        this.iframePricelist.setContent(amedia);
               
        component.setAttribute("controller", this);
    }


    public void onClick$menuitemPricelistSave(Event event) throws Exception {
        this.pricelistSave();
    }

    public void onClick$toolbarbuttonPricelistSave(Event event) throws Exception {
        this.pricelistSave();
    }

    private void pricelistSave() throws Exception {
        Media media = new Media();
        media.setByteData(this.amedia.getByteData());
        media.setName(this.amedia.getName());
        media.setContentType(this.amedia.getContentType());
        media.setDescription("File generated automatically from system.");
        this.mediaService.save(media,
                SecurityContextHolder.getContext().getAuthentication().getName());
        Messagebox.show("Saved successfully!");
    }
}
